﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Using

using System.ComponentModel;
using System.Windows;
using System.Windows.Media;

#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls.PieChart
{
    /// <summary>
    /// Interaction logic for SurfaceUserControl1.xaml
    /// </summary>
    public partial class SimplePieChart 
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #region FirstValue
        /// <summary>
        /// DependencyProperty for FirstValue.
        /// </summary>
        public static readonly DependencyProperty FirstValueProperty =
            DependencyProperty.Register("FirstValue",
                                        typeof(double?),
                                        typeof(SimplePieChart),
                                        new PropertyMetadata(1.0, OnValuePropertyChanged));
        /// <summary>
        /// Gets or sets if the first value in the SimplePieChart.
        /// </summary>
        /// <value>An amount</value>
        public double? FirstValue
        {
            get { return (double?)GetValue(FirstValueProperty); }
            set
            {
                SetValue(FirstValueProperty, value);
                Calculate();
            }
        }
        #endregion

        #region SecondValue
        /// <summary>
        /// DependencyProperty for SecondValue.
        /// </summary>
        public static readonly DependencyProperty SecondValueProperty =
            DependencyProperty.Register("SecondValue",
                                        typeof(double?),
                                        typeof(SimplePieChart),
                                        new PropertyMetadata(0.0, OnValuePropertyChanged));

        /// <summary>
        /// Gets or sets if the second value in the SimplePieChart.
        /// </summary>
        /// <value>An amount</value>
        public double? SecondValue
        {
            get { return (double?)GetValue(SecondValueProperty); }
            set
            {
                SetValue(SecondValueProperty, value);
                Calculate();
            }
        }
        #endregion


        #region BackColor
        /// <summary>
        /// DependencyProperty for BackColor.
        /// </summary>
        public static readonly DependencyProperty BackColorProperty =
            DependencyProperty.Register("BackColor",
                                        typeof(Brush),
                                        typeof(SimplePieChart),
                                        new PropertyMetadata(Brushes.White));

        /// <summary>
        /// Gets or sets if the background color in the SimplePieChart.
        /// </summary>
        /// <value>Brush</value>
        public Brush BackColor
        {
            get { return (Brush)GetValue(BackColorProperty); }
            set
            {
                SetValue(BackColorProperty, value);
            }
        }
        #endregion

        #region FirstColor
        /// <summary>
        /// DependencyProperty for FirstColor.
        /// </summary>
        public static readonly DependencyProperty FirstColorProperty =
            DependencyProperty.Register("FirstColor",
                                        typeof(Brush),
                                        typeof(SimplePieChart),
                                        new PropertyMetadata(Brushes.Red, OnValuePropertyChanged));

        /// <summary>
        /// Gets or sets if the second color in the SimplePieChart.
        /// </summary>
        /// <value>Brush</value>
        public Brush FirstColor
        {
            get { return (Brush)GetValue(FirstColorProperty); }
            set
            {
                SetValue(FirstColorProperty, value);
            }
        }
        #endregion

        #region SecondColor
        /// <summary>
        /// DependencyProperty for SecondColor.
        /// </summary>
        public static readonly DependencyProperty SecondColorProperty =
            DependencyProperty.Register("SecondColor",
                                        typeof(Brush),
                                        typeof(SimplePieChart),
                                        new PropertyMetadata(Brushes.Green, OnValuePropertyChanged));

        /// <summary>
        /// Gets or sets if the second color in the SimplePieChart.
        /// </summary>
        /// <value>Brush</value>
        public Brush SecondColor
        {
            get { return (Brush)GetValue(SecondColorProperty); }
            set
            {
                SetValue(SecondColorProperty, value);
            }
        }
        #endregion

        #endregion

        #region Constructors / Destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="SimplePieChart"/> class.
        /// </summary>
        public SimplePieChart()
        {

            if (!DesignerProperties.GetIsInDesignMode(this))
            {

                InitializeComponent();
                /*
                DependencyPropertyDescriptor heightProperty = DependencyPropertyDescriptor.FromProperty(HeightProperty,
                                                                                                        typeof (
                                                                                                            SimplePieChart
                                                                                                            ));
                DependencyPropertyDescriptor widthProperty = DependencyPropertyDescriptor.FromProperty(WidthProperty,
                                                                                                       typeof (
                                                                                                           SimplePieChart
                
                                                                                                           ));
                */
                this.SizeChanged += new SizeChangedEventHandler(OnSimplePieChartSizeChanged);
                /*
                heightProperty.AddValueChanged(this, OnSizeChanged);
                widthProperty.AddValueChanged(this, OnSizeChanged);
                */

            }
        }

       
      
        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        private void Calculate()
        {
            if (Piece1 == null || Piece2 == null)
            {
                return;
            }
            const double fullAngle = 359;
            double total = FirstValue.GetValueOrDefault(0) + SecondValue.GetValueOrDefault(0);
            if (total <= 0) total = 1;


            Piece1.RotationAngle = 0;
            Piece1.WedgeAngle = (FirstValue.GetValueOrDefault(0)/total)*fullAngle;
            Piece2.RotationAngle = Piece1.WedgeAngle;
            Piece2.WedgeAngle = (SecondValue.GetValueOrDefault(0)/total)*fullAngle;

        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((SimplePieChart) d).Calculate();
        }

        void OnSimplePieChartSizeChanged(object sender, SizeChangedEventArgs e)
        {
            Piece1.CentreX = Width / 2;
            Piece1.CentreY = Height / 2;
            Piece1.Radius = ((Width > Height) ? Height : Width) / 2;

            Piece2.CentreX = Width / 2;
            Piece2.CentreY = Height / 2;
            Piece2.Radius = ((Width > Height) ? Height : Width) / 2;

        }


        #endregion
    }
}